//
//  ForthViewController.m
//  MyCarsCanFly
//
//  Created by 何东东 on 15/9/22.
//  Copyright © 2015年 何东东. All rights reserved.
//

#import "ForthViewController.h"
#import "ListCell.h"
#import "MJRefresh.h"
#import "HTTPTOOL.h"
#import "ActivityDetailViewController.h"
#import "UMSocial.h"
#import "UIScrollView+MJRefresh.h"
#import "YZLBaseViewController.h"

@interface ForthViewController ()<UITableViewDataSource, UITableViewDelegate, listCellDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *listArr;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIView *animation;

@end

@implementation ForthViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"活动信息";
    [self creatTableView];
    [self handleData];
    
    //返回
    YZLBaseViewController *vc = [[YZLBaseViewController alloc] init];
    
//    [vc skipReturnWithTarget:self];
    
    //bar颜色
    [vc navigationbarColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(button1)];

}

- (void)button1 {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)creatAnimation {
    
    self.animation = [[UIView alloc] initWithFrame:self.view.frame];
    [self.tableView addSubview:self.animation];
    [self.animation setBackgroundColor:[UIColor whiteColor]];
  
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-90, self.view.frame.size.height/2.1, self.view.frame.size.width/2, self.view.frame.size.height/12)];
    
    label.textAlignment = 1;
    label.text = @"加载中...";
    label.font = [UIFont boldSystemFontOfSize:17];
    [self.animation addSubview:label];
    
    NSMutableArray *arr = [NSMutableArray array];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 65, self.view.frame.size.height / 2 -100, self.view.frame.size.width / 375 * 130, self.view.frame.size.height / 667 *130)];
    for (NSInteger i = 1; i < 21; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png" ,i + 100]];
        [arr addObject:image];
    }
    [self.animation addSubview:imageView];
    imageView.animationImages = arr;
    imageView.animationDuration = 2;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
   
    
}


- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ListCell class] forCellReuseIdentifier:@"reuse"];

    
//    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(takeNew:)];
//    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(takeNext:)];

}


#pragma mark - 数据处理
- (void)handleData {
    
    NSString *str = @"http://a.xcar.com.cn/interface/6.0/getEventList.php";
    
    self.modelArr = [NSMutableArray array];
    self.listArr = [NSMutableArray array];
    
    /* 调用HTTPTOOL方法来处理接口数据 */
    [HTTPTOOL GETWithURL:str body:nil httpHead:nil responseStyle:JSON success:^(id result) {
       
        self.arr = [NSMutableArray arrayWithObject:result];
        NSMutableArray *arrr = [NSMutableArray array];
        for (arrr in self.arr) {
            for (NSDictionary *dic in arrr) {
                
                ListModel *model = [ListModel ListModelWithDictionary:dic];
                [self.modelArr addObject:model];
            }
        }
        /* tableView重新加载数据 */
        [self.tableView reloadData];
        [self.animation removeFromSuperview];
    } fail:^(NSError *error) {
        
//        NSLog(@"网络错误");
        
    }];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityDetailViewController *activity = [[ActivityDetailViewController alloc] init];
    ListModel *model = [self.modelArr objectAtIndex:indexPath.row];
    activity.model = model;
    
    [self.navigationController pushViewController:activity animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArr.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
//    if (cell == nil) {
//        cell = [[[ListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuse"] autorelease];
//    }
    ListModel *model = [self.modelArr objectAtIndex:indexPath.row];
    
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    /* 为cell添加动画效果 */
    cell.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];

    return cell;
}

/* 返回cell的高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.view.frame.size.width * 0.8;
}

#pragma mark -- 实现分享的协议方法；
- (void)fenxiang {
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"561cb0a267e58ed7c2004b02"
                                      shareText:@"爱车族活动详情请登录爱车族客户端"
                                     shareImage:[UIImage imageNamed:@"zan"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:nil];
}



/* 下拉  刷新 */
//- (void)takeNew:(MJRefreshLegendHeader *)header
//{
//    [self.tableView reloadData];
//    [self.tableView.header endRefreshing];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
